import store from '@/store'
const state = {
  visitedViews: [],
  cachedViews: [],
  idViews: []
}

const mutations = {
  ADD_VISITED_VIEW: (state, view) => {
    if (view.meta && view.meta.noMenu) {
      const menu = state.idViews.includes(view.meta.id)
      if (!menu) {
        state.idViews.push(view.meta.id)
        state.visitedViews.push(view)
      }
    } else {
      const tag = state.idViews.includes(view)
      if (!tag) {
        state.idViews.push(view)
        const views = store.getters.route_list
        var item = views.find(v => v.id === view)
        if (item) {
          state.visitedViews.push(item.children[0])
        }
      }
    }
  },
  ADD_CACHED_VIEW: (state, view) => {
    if (view.meta && view.meta.noMenu) {
      const menu = state.cachedViews.includes(view.meta.title)
      if (!menu) {
        state.cachedViews.push(view.meta.title)
      }
    } else {
      const views = store.getters.route_list
      views.forEach(v => {
        if (v.id === view) {
          const tag = state.cachedViews.includes(v.children[0].name)
          if (!tag) {
            state.cachedViews.push(v.children[0].name)
          }
        }
      })
    }
  },
  // 删除当前
  DEL_VISITED_VIEW: (state, view) => {
    state.idViews.forEach((v, index) => {
      if (v === view) {
        state.idViews.splice(index, 1)
      }
    })
    state.visitedViews.forEach((v, index) => {
      if (v.meta.id === view) {
        state.visitedViews.splice(index, 1)
      }
    })
  },
  // 删除当前name
  DEL_CACHED_VIEW: (state, view) => {
    const views = store.getters.route_list
    views.forEach(v => {
      if (v.id === view) {
        const tag = state.cachedViews.includes(v.children[0].name)
        if (tag) {
          state.cachedViews.forEach((item, index) => {
            if (item === v.children[0].name) {
              state.cachedViews.splice(index, 1)
            }
          })
        }
      }
    })
  },
  // 删除其他
  DEL_OTHERS_VISITED_VIEWS: (state, view) => {
    // console.log(view)
    state.visitedViews = state.visitedViews.filter(v => {
      return v.meta.affix || v.meta.id === view.meta.id
    })
    state.idViews = []
    state.idViews.push(view.meta.id)
  },
  // 删除其他name
  DEL_OTHERS_CACHED_VIEWS: (state, view) => {
    for (const i of state.cachedViews) {
      if (i === view.name) {
        const index = state.cachedViews.indexOf(i)
        state.cachedViews = state.cachedViews.slice(index, index + 1)
        break
      }
    }
  },
  // 删除全部
  DEL_ALL_VISITED_VIEWS: state => {
    state.idViews = []
    state.visitedViews = []
  },
  // 删除全部name
  DEL_ALL_CACHED_VIEWS: state => {
    state.cachedViews = []
  }

}

const actions = {
  addView({ dispatch }, view) {
    dispatch('addVisitedView', view)
    dispatch('addCachedView', view)
  },
  addVisitedView({ commit }, view) {
    commit('ADD_VISITED_VIEW', view)
  },
  addCachedView({ commit }, view) {
    commit('ADD_CACHED_VIEW', view)
  },
  delOthersViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delOthersVisitedViews', view)
      dispatch('delOthersCachedViews', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delOthersVisitedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_VISITED_VIEWS', view)
      resolve([...state.visitedViews])
    })
  },
  delOthersCachedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_CACHED_VIEWS', view)
      resolve([...state.cachedViews])
    })
  },
  delAllViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delAllVisitedViews', view)
      dispatch('delAllCachedViews', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delAllVisitedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_VISITED_VIEWS')
      resolve([...state.visitedViews])
    })
  },
  delAllCachedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_CACHED_VIEWS')
      resolve([...state.cachedViews])
    })
  },
  delView({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delVisitedView', view)
      dispatch('delCachedView', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delVisitedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_VISITED_VIEW', view)
      resolve([...state.visitedViews])
    })
  },
  delCachedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_CACHED_VIEW', view)
      resolve([...state.cachedViews])
    })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
